package com.zk.stock.service.serviceimpl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.zk.stock.mapper.SysUserMapper;
import com.zk.stock.pojo.entity.SysUser;
import com.zk.stock.service.UserService;
import com.zk.stock.utils.IdWorker;
import com.zk.stock.vo.resp.LoginRespVo;
import com.zk.stock.vo.resp.R;
import com.zk.stock.vo.resp.ResponseCode;
import com.zk.stock.vo.resq.LoginReqVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 曹自康的Laptop
 * @version 1.0
 * @Date 2024-06-288:54
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl implements UserService {
    private final SysUserMapper sysUserMapper;
    private final IdWorker idWorker;
    private final StringRedisTemplate stringRedisTemplate;

    private final PasswordEncoder passwordEncoder;
    @Override
    public SysUser findByUserName(String userName) {
        return sysUserMapper.findUserInfoByUserName(userName);
    }

    @Override
    public List<SysUser> findUserAllList() {
        return  sysUserMapper.findUserAllList();
    }

    /**
     * 登录
     *
     * @param vo
     **/
    @Override
    public R<LoginRespVo> login(LoginReqVo vo) {
        if (vo == null || StringUtils.isBlank(vo.getUsername()) || StringUtils.isBlank(vo.getPassword()) || StringUtils.isBlank(vo.getCode())) {
            return R.error(ResponseCode.DATA_ERROR);
        }
        if(StringUtils.isBlank(vo.getCode()) || StringUtils.isBlank(vo.getSessionId())){
            return R.error(ResponseCode.CHECK_CODE_ERROR);
        }


        SysUser dnUser = sysUserMapper.findUserInfoByUserName(vo.getUsername());
        if (dnUser == null) {
            return R.error(ResponseCode.ACCOUNT_NOT_EXISTS);
        }

        if (!passwordEncoder.matches(vo.getPassword(),dnUser.getPassword())) {
            return R.error(ResponseCode.USERNAME_OR_PASSWORD_ERROR);

        }
        LoginRespVo loginRespVo = new LoginRespVo();

        BeanUtils.copyProperties(dnUser,loginRespVo);

        stringRedisTemplate.opsForValue().set("name","曹自康",30, TimeUnit.SECONDS);


        return R.ok(loginRespVo);
    }

    /**
     * 生成图片验证码功能
     *
     * @return
     */
    @Override
    public R<Map> getCaptcha() {
        // 1.生成图片验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(250, 40, 4, 5);

        // 2.获取校验码
        String checkCode = lineCaptcha.getCode();
        // 经过base64编码 处理的图片数据
        String imageBase64 = lineCaptcha.getImageBase64();

        // 3.生成sessionid 转成String 避免前端的精度丢失问题
        String sessionId = String.valueOf(idWorker.nextId());
        log.info("生成的图片校验码：{}",checkCode);
        stringRedisTemplate.opsForValue().set("ck:"+sessionId,checkCode,5,TimeUnit.MINUTES);
        // 4组装数据
       Map<String,String> data =  new HashMap();
       data.put("imageData",imageBase64);
       data.put("sessionId",sessionId);


       // 5.响应数据
        return R.ok(data);
    }
}
